//
//  HRBlackListViewController.m
//  Hear
//
//  Created by mac on 2017/4/21.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HRBlackListViewController.h"
#import "HRBlackListModel.h"
#import "HRBlackListViewModel.h"
#import "HRBlackListCell.h"
#import "MJRefresh.h"
#import "HRBlackListModel.h"
#import <DZNEmptyDataSet/UIScrollView+EmptyDataSet.h>

@interface HRBlackListViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)HRBlackListViewModel *viewModel;
@property (nonatomic, strong)NSMutableArray *contentArray;
@property (nonatomic, assign)NSInteger pIndex;

@end

@implementation HRBlackListViewController

-(HRBlackListViewModel *)viewModel{
    if (_viewModel == nil){
        _viewModel = [HRBlackListViewModel new];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"黑名单";
    self.view.backgroundColor = [UIColor whiteColor];
    self.contentArray = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 70;
    _tableView.emptyDataSetDelegate = self;
    _tableView.emptyDataSetSource = self;
    
    
    
//    UIButton *leftButton = [UIButton buttonWithType:0];
//    leftButton.size = CGSizeMake(20, 20);
//    [leftButton setImage:[UIImage imageNamed:@"Back-Arrow"] forState:UIControlStateNormal];
//    [leftButton addTarget:self action:@selector(clickMenu) forControlEvents:UIControlEventTouchUpInside];
//    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
//    self.navigationItem.leftBarButtonItem = leftItem;
    
    
    [self getDynamicData];

}

- (void)clickMenu{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"空白"];
}
- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state{
    NSString *text = nil;
    UIFont *font = nil;
    UIColor *textColor = nil;
    text = @"暂无拉黑任何人";
    font = CustomUIFont(16);
    textColor = FirstTextColor;
    NSMutableDictionary *attributes = [NSMutableDictionary new];
    if (font) [attributes setObject:font forKey:NSFontAttributeName];
    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}


#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.contentArray.count;
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    HRBlackListCell *cell = [HRBlackListCell cellWithTableView:tableView];
    
    HRBlackListModel *model = self.contentArray[indexPath.row];
    if (model)
    {
        cell.blackModel = model;
    }
    //取消cell的点击状态
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    <#model#> *mode = <#array#>[indexPath.row];
//    
//    <#VC#> *<#VC#> = [<#VC#> new];
//    [self.navigationController pushViewController:<#VC#> animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source.
        HRBlackListModel *model = self.contentArray[indexPath.row];
        [self.viewModel deleteBlackListContentModel:@{@"LocalId":model.LocalId,@"UserLocalId":USERID} seccess:^(BOOL seccess) {
            if (seccess){
                [self.contentArray removeObject:model];
                [self.tableView reloadData];
            }
        }];
        
        EMError *error = [[EMClient sharedClient].contactManager removeUserFromBlackList:model.LocalId];
        if (!error) {
            DDLog(@"IM=============移除配名单成功");
        }
    }
    
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"取消";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)getDynamicData{
    self.pIndex = 0;
    
    [self getContentData:YES];
    WeakSelf
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.pIndex = 0;
        [self getContentData:YES];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
    
    //    //上拉加载
    self.tableView.mj_footer=[MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.pIndex ++;
        [weakSelf getContentData:NO];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
    
}

- (void)getContentData:(BOOL)isHead{
    HRBlackListRequestModel *requestModel = [HRBlackListRequestModel new];
    requestModel.InformantUser = USERID;
    requestModel.PageSize = 10;
    requestModel.PageIndex = self.pIndex;
    WeakSelf
    [self.viewModel getBlackListContentModel:requestModel resultArr:^(NSArray *resultArr) {
        
        if (isHead){
            [self.contentArray removeAllObjects];
        }
        [self.contentArray addObjectsFromArray:resultArr];
        [weakSelf.tableView reloadData];
        if (resultArr.count < requestModel.PageSize){
            weakSelf.tableView.mj_footer.hidden = YES;
        }else{
            
            weakSelf.tableView.mj_footer.hidden = NO;
        }
    }];
    
}



@end
